package solutions.leetcode.easy;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/search-in-rotated-sorted-array/">33. 搜索旋转排序数组</a>
 */
public class Solution33 {

    public int search(int[] nums, int target) {
        int l = 0, r = nums.length - 1;
        while (l <= r) {
            int mid = (l + r) / 2;
            if (target > nums[mid]) {
                if (nums[l] > nums[mid] && target > nums[r]) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            } else if (target < nums[mid]) {
                if (nums[l] < nums[mid] && target < nums[l]) {
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Solution33 solution = new Solution33();
        System.out.println(solution.search(new int[]{
                5, 1, 3
        }, 3));
    }
}